package cn.edu.ctbu.sbadmin.teacher.webapi;

import cn.edu.ctbu.sbadmin.common.controller.BaseRestController;
import cn.edu.ctbu.sbadmin.common.utils.MQuery;
import cn.edu.ctbu.sbadmin.common.utils.MQueryHelper;
import cn.edu.ctbu.sbadmin.common.utils.PageUtils;
import cn.edu.ctbu.sbadmin.common.utils.R;
import cn.edu.ctbu.sbadmin.lablog.domain.DimClassDO;
import cn.edu.ctbu.sbadmin.lablog.service.DimClassService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("webapi/teacher/dimclass")
public class TeacherClassRestController extends BaseRestController {


    @Autowired
    DimClassService dimclassService;

    @RequestMapping("/getByPage")
    @RequiresPermissions("teacher:dimclass:list")
    public PageUtils list(@RequestParam("where") String where,
                          @RequestParam("pageSize") Integer pageSize,
                          @RequestParam("pageIndex") Integer pageIndex,
                          @RequestParam("sort") String sort,
                          @RequestParam("direct") String direct
    ) throws Exception {
        //首先，把where条件的参数转为QueryAndItems
//        其中,where条件需要我们自己组装.示例:
//        name=张^:like&time=2^:l g&dmtCreate=2017-01-01^^2018-01-01
//        这个表示:
//        (name like '%张%' )and ( time<2 ) and (dmtCreate between '2017-01-01' and '2018-01-01)

        Long userId=super.getUserId();
        where="teacherId="+userId+"&"+where;

        MQuery mQuery= MQueryHelper.GenQuery(where,pageSize,pageIndex,sort,direct,true);

        List<DimClassDO> dimclassDOList = dimclassService.listView(mQuery);

        int total=dimclassService.countView(mQuery);
        PageUtils pageUtils = new PageUtils(dimclassDOList,total);

        return pageUtils;
    }

    @RequestMapping("/get")
    @RequiresPermissions("teacher:dimclass:list")
    public R get(Long id) throws Exception{

        Long userId=super.getUserId();
        MQuery mQuery= MQueryHelper.GenQuery("teacherId="+userId+"&dim_class.id="+id+"^:=",10,0,"","",true);
        List<DimClassDO> dimClassDOList=dimclassService.listView(mQuery);

        if(dimClassDOList.size()>0)
            return R.ok("data",dimClassDOList.get(0));

        return R.error(-1,"找不到数据");
    }

    @RequestMapping("/save")
    @RequiresPermissions("teacher:dimclass:add")
    public R save(DimClassDO dimclassDO){

        Long userId=super.getUserId();
        dimclassDO.setTeacherId(userId);
        dimclassDO.setCreateTime(new Date());
        if(dimclassService.save(dimclassDO)>0)
            return R.ok("data",dimclassDO);
        return R.error();

    }

    @RequestMapping("/update")
    @RequiresPermissions("teacher:dimclass:edit")
    public R update(DimClassDO dimclassDO){

        Long userId=super.getUserId();



        DimClassDO oldClassDO=dimclassService.get(dimclassDO.getId());




        if(userId==oldClassDO.getTeacherId()){

            dimclassDO.setCreateTime(oldClassDO.getCreateTime());
            dimclassDO.setTeacherId(oldClassDO.getTeacherId());

            if(dimclassService.update(dimclassDO)>0){
                return R.ok("data",dimclassDO);
            }
        }

        return R.error(-3,"你无权更新不属于你的数据");
    }

    @RequestMapping("/batchDelete")
    @RequiresPermissions("teacher:dimclass:remove")
    public R batchDelete(@RequestParam("ids[]") Long[] ids){


        List<DimClassDO> dimclassDOS=dimclassService.findByIds(ids);

        Long userId=super.getUserId();
        if(dimclassDOS.size()>0){
            for(DimClassDO dimclassDO:dimclassDOS){
                if(dimclassDO.getTeacherId()==userId){
                    dimclassService.delete(dimclassDO.getId());
                }
            }
        }
        return R.ok();
    }
}
